Skip to content

Conversation

nileshvyas
Copy link

script: footprint: Improve C++ static variable reporting

This commit enhances the size_report script to correctly categorize static C++ variables in the memory report tree. Previously, these variables were incorrectly grouped under the "No paths" category.

Problem Description

The size_report.py script uses DWARF debug information and ELF symbol tables to map memory usage to source file paths. While this system works well for C and most C++ functions, a specific limitation exists for C++ static variables.

  • Functions vs. Variables: In DWARF, functions (DW_TAG_subprogram) are typically defined by an address range (DW_AT_low_pc to DW_AT_high_pc). In contrast, variables (DW_TAG_variable) are represented by a single, specific address (DW_AT_location).
  • Legacy Logic: The script's address-based matching logic was primarily designed to handle address ranges. As a result, it was unable to properly correlate the single-address variables with their source file paths, causing them to be categorized as having "no path."

This behaviour gives a misleading view of memory usage for C++ applications with static members, as a significant portion of the memory footprint could be incorrectly attributed to the "No paths" section.

Solution

This change introduces a more robust address-based mapping system within the do_address_range_matching function.

  1. Expanded Matching: The function is modified to iterate over both functions (DW_TAG_subprogram) and variables (DW_TAG_variable) from the DWARF information.
  2. Single-Address Handling: A dedicated check is added to correctly handle variables by matching the symbol's address to the variable's single DW_AT_location address.

This solution ensures that static C++ variables are correctly mapped to their source files by using the most reliable piece of information available: the memory address. This approach is more robust than name-based matching, as it bypasses C++'s name mangling complexities and handles all symbols based on their location in memory.

Impact

This enhancement provides a more accurate and detailed memory footprint report for C++ applications, allowing developers to better understand and optimize memory usage. Static C++ variables will now be correctly nested under their respective file paths in the report tree, providing a clearer overview of the binary's composition.

Enhance the `size_report` script to correctly categorize static C++
variables in the memory report.

Static C++ variables are currently listed under the "No paths"
category in the size report. This occurs because the script's
address-based mapping logic is designed for functions, which
occupy an address range, but fails to handle variables that
have only a single memory address.

This commit improves the `do_address_range_matching` function
to handle single-address mappings. The updated logic now correctly
identifies and assigns static C++ variables to their corresponding
source file paths in the report tree, providing a more accurate and
comprehensive memory footprint analysis for C++ applications.

This enhancement ensures that all symbols, regardless of their
linkage or type, are correctly placed in the report, providing a
more accurate overview of memory usage.

Signed-off-by: Nilesh Vyas <[email protected]>
@nileshvyas nileshvyas force-pushed the size_report-script-cpp-symbol-path branch from 5ecfad5 to 4cf0da6 Compare October 3, 2025 05:48
Copy link

sonarqubecloud bot commented Oct 3, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant